import { RootState } from '@/store'
import { useSelector } from 'react-redux'
import { Navigate, useLocation } from 'react-router-dom'

export default function Auth ({ children }: { children: JSX.Element }) {
  const token = useSelector((state: RootState) => state.user.token)
  const location = useLocation()

  // 判断是否登录，如果登录，则还是访问它想去的页面，如果不是，则跳转到登录页面
  if (token) {
    return children
  } else {
    return <Navigate to={`/login?redirect=${location.pathname}`} />
  }
}
